package com.pro.springboot.controller;


import com.pro.springboot.interfacep.MultiThreadiInteface;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 从山东省推送的文件中导入统一社会信用代码信息
 * @Author: 张和祥
 * @CreateDate: 2018/5/11 15:46
 * @Version: 1.0
 */
public class ImportCreditCodeFromXml extends MultiThreadiInteface<Map<String, Object>> {
  //  private static final MySqlBaseDao MYSQL_BASE_DAO = new MySqlBaseDao("db0_");
    private static int addNum = 0;
    private static int removeNum = 0;
    private static Map<String, String> relateMap = new HashMap<>();

    static {
        relateMap.put("entname", "entName");
        relateMap.put("credcode", "credCode");
        relateMap.put("regcode", "regCode");
        relateMap.put("orgcode", "orgCode");
    }

    private ImportCreditCodeFromXml(int threadNum) {
        initMultiThread(threadNum);
    }

    public static void main(String[] args) {

        try {
//            String rootPath = ConfigModel.CONFIG.getProperty("creditCodeFolderPath", "d:\\import"); // 文件夹目录
//            String threadNum = ConfigModel.CONFIG.getProperty("threadNum", "10"); // 开启线程数
        	String rootPath = "";
        	String threadNum = "";
        	ImportCreditCodeFromXml xmlPort = new ImportCreditCodeFromXml(Integer.parseInt(threadNum)); // 开启线程数
            xmlPort.parseXmlToQueue(rootPath, relateMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void parseXmlToQueue(String rootPath, Map<String, String> relateMap) throws Exception {
        File rootFile = new File(rootPath);
        if (rootFile.isDirectory()) {
            String[] fileList = rootFile.list();
            if (fileList != null && fileList.length > 0) {
                long startTime = System.currentTimeMillis();
                for (int i = 0; i < fileList.length; i++) {
                    String fileLastPath = fileList[i];
                    String filePath = rootPath + "\\" + fileLastPath;
                    if (StringUtils.isNotBlank(filePath)) {
                        File file = new File(filePath);
                        if (file.exists()) {
                            SAXReader reader = new SAXReader();
                            org.dom4j.Document document = reader.read(file);
                            Element root = document.getRootElement();
                            List<Element> rowElements = root.elements();
                            if (rowElements != null && rowElements.size() > 0) {
                                for (Element rowElement : rowElements) {
                                    List<Element> attributeElements = rowElement.elements();
                                    if (attributeElements != null && attributeElements.size() > 0) {
                                        Map<String, Object> map = new HashMap<>();
                                        for (Element attributeElement : attributeElements) {
                                            String elementName = attributeElement.getName();
                                            String elementValue = attributeElement.getText();
                                            String key = relateMap.get(elementName);
//                                            if (StringUtils.isNotBlank(key)) {
//                                                map.put(key, elementValue);
//                                            }
                                        }
                                        linkedQueue.put(map);
                                    }
                                }
                                addNum += rowElements.size();
//                                log.warn("第" + (i + 1) + "个xml解析推送完成,共推送【" + rowElements.size() + "】条...");
                            }
                        } else {
//                            log.warn("第" + (i + 1) + "个xml文件不存在...");
                        }
                    } else {
//                        log.warn("文件路径为空...");
                    }
                }
                if (finish4Thread()) {
                    Long endTime = System.currentTimeMillis();
//                    log.warn("数据添加完成,共耗时【" + (endTime - startTime) + "】毫秒,其中共获取数据【" + addNum + "】条，被移除的【" + removeNum + "】，新增【"
//                            + (addNum - removeNum) + "】条。");
                }
            }
        }
    }

    @Override
    public void customer() {
        try {
            Map<String, Object> map = linkedQueue.take();
//            CreditCodeModel obj = ModelUtil.map2model(map, CreditCodeModel.class);
//            obj.setId(CmUtil.getPkId());
//            MYSQL_BASE_DAO.save(obj);
        } catch (Exception e) {
//            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void recovery() {

    }
}
